<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   注册登录
 */
defined('IN_YuLin') || exit('NO PERMIT!');

if($_uid)
{
	header('Location: '.U('member'));
	exit;
}

if(IS_POST)
{	
    $username	=	NoBadStr($_POST['username']);
    $password	=	NoBadStr($_POST['password']);
    $password2	=	NoBadStr($_POST['repassword']);
    $verify		=	NoBadStr($_POST['verify']);
    include_once LIB_PATH."class/Secoder.class.php";
	(!Secoder::check($verify)) && AjaxReturn(1,'验证码错误');
	
	(strlen($username) < 2 || strlen($username) > 16) && AjaxReturn(2,'账号为2-16位');
	(strlen($password) < 2 || strlen($password) > 20) && AjaxReturn(2,'密码为2-20位');
	($password != $password2) && AjaxReturn(2,'确认密码不正确');
	
    $user = $db->getrow('SELECT 1 FROM '.Table('user').' WHERE username = \''.$username.'\'');
	$user && AjaxReturn(2,'该账号已经被注册');
	$invitaCode = NoBadStr($_POST['invitation']);
	// 检查邀请码是否使用过
	$codeInfo  = $db->getrow('SELECT id, uid, remark, status, type, is_once FROM ' . Table('user_invita_code') . ' WHERE invita_code = "' . $invitaCode . '"');
	if (!$codeInfo) AjaxReturn(2, '邀请码不正确');
	if ($codeInfo['is_once'] == '2'){
	    // 如果是一次性的，检查是否有使用过
	    $checkIsUse     = $db->getrow('SELECT id FROM ' . Table('user_info') . ' WHERE invita_code_id = ' . $codeInfo['id']);
	    if ($checkIsUse['id']) AjaxReturn(2,'单次邀请码，已使用过');
	}
	if ($codeInfo['status'] == '2'){
	    if ($checkIsUse['id']) AjaxReturn(2,'邀请码无效');
	}
	
	//	插入数据库
	$ins = [];
	$ins['username']	=	$username;
	$ins['password']	=	Password($password);
	$ins['reg_time']	=	TIMESTAMP;
	$ins['reg_ip']		=	$_Y['ip'];
	$ins['last_time']	=	TIMESTAMP;
	$ins['last_ip']		=	$_Y['ip'];
	$ins['money']		=	10000;	//	注册赠送1w
	$ins['status']		=	1;
	// 注册是设置随机头像
	$ins['head_pic']    = 'static/images/head_pic/1_(' . rand(1, 102) . ').jpg';
	$id = $db->add('user',$ins);
	!$id && AjaxReturn(1,'注册失败，请重新注册');
	
	// 检查用户的等级头衔  ==========================
	$sql        = 'SELECT id FROM ' . Table('user_level') . ' WHERE status = 1 ORDER BY group_point ASC limit 1';
	$levelId    = $db->getrow($sql);
	
	$ins2 = [];
	$ins2['uid']	=	$id;
	$ins2['truename']=	$realname;
	$ins2['mobile']	=	$phone;
	$ins2['qq']		=	$qq;
	$ins2['invita_code_id'] = $codeInfo['id'];
	$ins2['user_type']      = $codeInfo['type'];
	if ($levelId){
	    $ins2['level_id'] = $levelId['id'];
	}
	$db->add('user_info',$ins2);
	
	// 当前注册一个用户， 对每个上面所有关系级别的用户的下级用户累加
    $userLineData = array();
    getUserLineUp($id, $userLineData);
    $fidStr = implode(',', array_keys($userLineData));
    $db->exec('UPDATE ' . Table('user_info') . ' SET lower_nums = lower_nums + 1 WHERE uid IN (' . $fidStr . ')');
	//	推荐人
	$upuid = intval($_GET['ruid']);
	!$upuid && $upuid = intval($_SESSION['upuid']);
	if($upuid)
	{
		$tmp = $db->getrow('SELECT 1 FROM '.Table('user').' WHERE id = '.$upuid);
		!$tmp && $upuid = 0;
	}
	$tmp = [];
	$tmp['uid']		=	$id;
	$tmp['upuid']	=	$upuid;
	$db->add('user_status',$tmp);
	$upuid && $db->exec('UPDATE '.Table('user_status').' SET sons = sons + 1 WHERE uid = '.$upuid);

    $_SESSION['uid'] = $id;
	set_cookie('LoginSessionID',session_id());
	
    AjaxReturn(0,'注册成功');
}
$inviteCode = NoBadStr($_GET['key']);
$tpl->display('member/register');